<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<script src="./js/jquery-1.9.1.js"></script>
<script src="./ibsheet/ibsheetinfo.js"></script>
<script src="./ibsheet/ibsheet.js"></script>
<script>
	$(document).ready(function() {
		//sheet 초기화  
		createIBSheet2(document.getElementById("viewArea"),"mySheet","100%","400px");
		
		var cfg = {SearchMode:smLazyLoad,Page:30};
		var headers = [
			{Text:"모델번호|평형|단가(A)|판매가(B)\nA * 1.03 / 100|현금구입가(C)\n A * 0.9 / 100|할부구입가(D)\nA * 0.93 / 100|현금부담액\nB-C|할부부담액\nB-D", Align:"Center"}
		];
		var info = {Sort:1,ColMove:1,ColResize:1,HeaderCheck:0};
		var cols = [
			{Type:"Text",Width:130},
			{Type:"Text",Width:100,ColMerge:0},
			{Type:"Int",Width:80,SaveName:"A", Align:"Right",ColMerge:0},
			{Type:"AutoSum",Width:85,SaveName:"B",Format:"#,##0.00",CalcLogic:"|A|*1.03/100", Align:"Right",ColMerge:0},
			{Type:"AutoSum",Width:85,SaveName:"C",Format:"#,##0.00",CalcLogic:"|A|*0.9/100", Align:"Right",ColMerge:0},
			{Type:"AutoSum",Width:85,SaveName:"D",Format:"#,##0.00",CalcLogic:"|A|*0.93/100", Align:"Right",ColMerge:0},
			{Type:"AutoAvg",Width:85,Format:"#,##0.00",CalcLogic:"|B|-|C|",ColMerge:1, Align:"Right",ColMerge:0},
			{Type:"AutoAvg",Width:85,Format:"#,##0.00",CalcLogic:"|B|-|D|", Align:"Right",ColMerge:0}
		];

		mySheet.SetConfig(cfg);
		mySheet.InitHeaders(headers,info);
		mySheet.InitColumns(cols);

		mySheet.FitColWidth();
		doAction('search');
	});
	
	var multiSubSum = false;		// 다중소계 여부를 구분한다.
	
	/*Sheet 각종 처리*/
	function doAction(sAction) {
		
		switch(sAction) {
			case "search":		//조회
				mySheet.DoSearch("./data/sheet_data2.xml");
			break;
				
			case "subsum":
		    	multiSubSum = false;
			    var info = [{StdCol:0, SumCols:"2|3|4|5", ShowCumulate:0, Sort:0, CaptionCol:0}];
			   	mySheet.ShowSubSum(info); 
			   	doAction('search');
		    break;

			case "subsum2":
		    	multiSubSum = true;
			    var info = [{StdCol:0, SumCols:"6|7", ShowCumulate:0, Sort:0},{StdCol:1, SumCols:"2|3|4|5", ShowCumulate:0, Sort:0, CaptionCol:1}];
			   	mySheet.ShowSubSum(info); 
			   	doAction('search');
		    break;
		}
	}
	
	function mySheet_OnSearchEnd(Code, Msg, StCode, StMsg) {
		try {
			mySheet.SetSumValue(0,"합 계/평 균");
			mySheet.SetCellAlign(mySheet.LastRow(), 0, "Center");
			mySheet.SetMergeCell(mySheet.LastRow(), 0, 1,2);
			
			var lr = mySheet.LastRow();
			
			// 다중소계 사용시 머지 시키기
			if (multiSubSum){
				var str = "";
				var startrow = 1;
				for (var i=1; i<lr; i++) {
					str = mySheet.GetCellText(i,0);			
					if (str.indexOf("소계:")>-1){
						mySheet.SetMergeCell(startrow,0,i-startrow,1);
						startrow=i+1;
					}
				}
			}
			
			mySheet.FitColWidth();
		} catch(e) {
			alert(e);		// 에러
		}
	}
	
</script>
<title>다중소계 예제</title>
</head>
<body>
<div id="main">
	<div id="title">다중소계 예제</div>
	<br>
	<input type="button" value="일반소계조회" onClick="doAction('subsum');" />
	<input type="button" value="다중소계조회" onClick="doAction('subsum2');" />
	<br>&nbsp;
	<div id="viewArea"></div>
</div>
</body>
</html>